//@dart=2.12
import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ls_work/res/colors.dart';
import 'common_widget.dart';

class TitleInputWidget extends StatelessWidget {
  String? title;
  String hint;
  CommonInputType keyboardType;
  ValueChanged<String>? onChanged;
  ValueChanged<String>? onSubmitted;
  bool isRow;
  int? contentWeight;
  int? titleWeight;
  bool isRequired;
  bool inEdit;
  String text;
  Widget? arrow;
  int? maxLength;

  TitleInputWidget(
      {this.title,
      this.hint = "请输入",
      this.keyboardType = CommonInputType.text,
      this.onChanged,
      this.onSubmitted,
      this.isRow = false,
      this.contentWeight,
      this.titleWeight,
      this.isRequired = false,
      this.inEdit = true,
        this.arrow,
      this.text = "",this.maxLength});

  @override
  Widget build(BuildContext context) {
    return isRow
        ? TitleContentWidget(
            title: title??"",
            arrow: arrow,
            child: inEdit
                ? NoBorderInput(
                    hintText: hint,
                    text: text,
                    keyboardType: keyboardType,
                    onChanged: onChanged,
                    onSubmitted: onSubmitted,
                    textAlign: TextAlign.end)
                : CommonText(text, hintText: hint, textAlign: TextAlign.end),
            contentWeight: contentWeight,
            titleWeight: titleWeight,
            isRequired: isRequired)
        : Column(
            children: [
              TitleContentWidget(
                  title: title??"",
                  isRequired: isRequired,
                  child: Container(),
                  arrow: arrow,
                  contentWeight: -1),
              (inEdit
                      ? NoBorderInput(
                          hintText: hint,
                          text: text,
                          keyboardType: keyboardType,
                          onChanged: onChanged,
                          maxLength: maxLength,
                          onSubmitted: onSubmitted)
                      : CommonText(text, hintText: hint))
                  .addToContainer(
                      width: double.infinity,
                      decoration: BoxDecoration(
                          color: Color(0xfff7f7f7),
                          borderRadius: 3.borderRadius),
                      padding:
                          EdgeInsets.symmetric(horizontal: 16, vertical: 12)),
              SizedBox(
                height: 16,
              )
            ],
          );
  }
}

class CheckInputWidget extends StatelessWidget {
  String title;
  bool isChecked;
  String text;
  ValueChanged<String>? onChanged;
  Function(bool checked)? onCheckChange;
  bool enable;
  bool inEdit;
  String hint;

  CheckInputWidget(
      {required this.title,
      this.isChecked = false,
      this.text = "",
      this.hint = '请输入',
      this.inEdit = true,
      this.onChanged,
      this.enable = true,this.onCheckChange});

  @override
  Widget build(BuildContext context) {
    return StatefulBuilder(builder: (context, setState) {
      return Row(
        children: [
          CheckTextItem(
            title: title,
            onChanged: (value) {
              setState(() {
                isChecked = value;
              });
              if(onCheckChange!=null){
                onCheckChange!(value);
              }
            },
            enable: enable,
            defaultSelected: isChecked,
            textStyle: TextStyle(color: Colours.text_dark),
          ).setWeight(1),
          isChecked && inEdit
              ? NoBorderInput(
                      text: text,
                      hintText: hint,
                      onChanged: onChanged,
                      maxLines: 1)
                  .addToContainer()
                  .setWeight(1)
              : CommonText(text, hintText: hint).addToContainer().setWeight(1)
        ],
      );
    });
  }
}

class TitleVerticalContent extends StatelessWidget {
  String title;
  Widget child;
  bool isRequired;

  TitleVerticalContent(
      {required this.title, required this.child, this.isRequired = false});

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        TitleContentWidget(
            title: title,
            isRequired: isRequired,
            child: Container(),
            contentWeight: -1),
        child,
        SizedBox(
          height: 15,
        ),
      ],
    );
  }
}

class CheckTextItem extends StatelessWidget {
  bool defaultSelected;
  String title;
  Function(bool value) onChanged;
  MainAxisAlignment mainAxisAlignment;
  Color? checkedColor;
  Color? fillColor;
  bool enable;
  TextStyle? textStyle;
  bool fill;
  bool showIcon;

  CheckTextItem(
      {this.defaultSelected = false,
      required this.title,
      required this.onChanged,
      this.mainAxisAlignment = MainAxisAlignment.start,
      this.checkedColor,
      this.fill = true,
      this.showIcon = true,
      this.fillColor,
      this.enable = true,
      this.textStyle});

  @override
  Widget build(BuildContext context) {
    return StatefulBuilder(builder: (context, setState) {
      return Row(
        mainAxisAlignment: mainAxisAlignment,
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          showIcon
              ? defaultSelected
                  ? Icon(
                      Icons.check_circle,
                      color: Color(0xff159BD5),
                      size: 20,
                    )
                  : Icon(
                      Icons.radio_button_off,
                      color: Colours.gray_99,
                      size: 20,
                    )
              : SizedBox(
                  width: 20,
                ),
          SizedBox(
            width: 5,
          ),
          !fill
              ? Text(
                  title,
                  style: textStyle ??
                      TextStyle(fontSize: 16, color: Colours.text_dark),
                ).addToContainer(
                  constraints: BoxConstraints(
                      maxWidth: MediaQuery.of(context).size.width - 100))
              : Text(
                  title,
                  style: textStyle ??
                      TextStyle(fontSize: 16, color: Colours.text_dark),
                )
                  .addToContainer(
                      constraints: BoxConstraints(
                          maxWidth: MediaQuery.of(context).size.width - 100))
                  .setWeight(1),
        ],
      ).onTap(() {
        if (enable) {
          setState(() {
            defaultSelected = !defaultSelected;
          });
          onChanged(defaultSelected);
        }
      });
    });
  }
}
